set seed 128818
*global threshold_rd = .2
*global threshold_fg = .01

cap program drop _all


cap program drop epost
program epost, eclass
	args 
	ereturn scalar pp = `e(p)'
	ereturn scalar p = `e(pv_rb)'
end


cap program drop drawgraph_sub
program drawgraph_sub, rclass
	* DAYS "90" "180"
	args outcomevar runningvar condition DAYS graph_i GRAPH TABLE buyer
	keep if abs(`runningvar') < . 
	if "`condition'" ~= ""{
		keep if `condition'
		local condition_f = ustrregexra("`condition'","[\s\=\!\.\,\&]","")
	}
	pause on
	foreach days in `DAYS'{
		cap drop flag_tmp
		gen flag_tmp = 0
		sum `runningvar' if `runningvar' < 0, d
		replace flag_tmp = 1 if `runningvar' < `r(p10)'
		sum `runningvar' if `runningvar' > 0, d
		replace flag_tmp = 1 if `runningvar' > `r(p90)' & `runningvar' < .
		*di "AAAA"
		di "`outcomevar'`days' "
		di "`runningvar'"
		rdrobust `outcomevar'`days' `runningvar' ///
			if `runningvar' ~= 0 & flag_tmp == 0, vce(nncluster contract_id)
		*di e(pv_rb)
		if _rc == 0{
			local tau = strofreal(`e(tau_bc)', "%9.2g")
			local pv = strofreal(`e(pv_rb)',"%9.2g")
			local obs = strofreal(`e(N_l)' + `e(N_r)',"%9.0fc")
			local hr = strofreal(`e(h_l)',"%9.2g")
			local hl = strofreal(`e(h_r)',"%9.2g")
			local se_tau = strofreal(`e(se_tau_rb)',"%9.2g")
			mat B_left =  e(beta_p_l)
			mat B_right =  e(beta_p_r)
		}
		local t = round(`days' / 30)
		if `t' > 1{
			local months = "s"
		}
		epost 
		
		*est sto t`outcomevar'`days'`runningvar'`condition_f'
		*local TABLE = "`TABLE' t`outcomevar'`days'`runningvar'`condition_f'"
		est sto t`buyer'_`days'
		local TABLE "`TABLE' t`buyer'_`days'"
		if `e(pv_rb)' <= .01{
			local star10 = "***"
		}
		else if `e(pv_rb)' <= .05{
			local star10 = "**"
		}
		else if `e(pv_rb)' <= .1{
			local star10 = "*"
		}
		else{
			local star10 = ""
		}
		preserve		/*added on 23Jun24 to calculate fdr*/
			local exists = 0
			cap use ../data/rd_results, clear
			if _rc == 0{
				qui count if (buyer == "`buyer'") & (days == `days') ///
				& (runningvar == "`runningvar'") & outcomevar == "`outcomevar'" ///
				& pv == `e(pv_rb)' & condition == "`condition'"
				local exists = r(N)
			}
			if `exists' ==0{
				clear
				gen buyer = "`folder'"
				gen days = `days'
				gen runningvar = "`runningvar'"
				gen outcomevar = "`outcomevar'"
				gen pv = `e(pv_rb)'
				gen tau =`e(tau_bc)'
				gen se =`e(se_tau_rb)'
				gen datetime = "$S_TIME  $S_DATE"
				gen condition = "`condition'"
				cap append using ../data/rd_results
				sleep 1000
				save ../data/rd_results, replace
			}
		restore
		*local star10 = " "
		if `graph_i' == 1{
			cap graph drop u`days'_`runningvar'
			rdplot `outcomevar'`days' `runningvar' if abs(`runningvar') < $threshold_fg & ///
			`runningvar' ~= 0,  ///
			graph_options(title("`days' Days", size(large)) name(u`days'_`runningvar') ///
				legend(off) xtitle( {&Delta}) ///
				subtitle("winners　　　　　losers", size(medium) ring(5) margin(zero)) ///
				/*ytitle(`outcomevar_label')*/ ylabel(, angle(0) format(%3.1g)) ///
				graphregion(color(white) lcolor(none)))
		}	
		local outcomevar_label : variable label `outcomevar'`days'
		if `graph_i' == 2{
			cap graph drop u`days'_`runningvar'
			rdplot `outcomevar'`days' `runningvar' if abs(`runningvar') < $threshold_fg & ///
						`runningvar' ~= 0, p(1) h( ) ///
			graph_options(title("`days' Days", size(large)) name(u`days'_`runningvar') legend(off) ///
				xtitle({&Delta}) graphregion(color(white) lcolor(none)) ///
				/*t2title("From_`start_day'" "Thru_`end_day'", ///
					size(small) place(nw) ring(1))*/ ///
				subtitle("winners　　　　　losers", size(medium) ring(5) margin(zero)) ///
				t2title("{it:{&beta}}({it:se}):`tau'(`se_tau')`star10'", ///
					size(small) place(ne) ring(5) margin(zero)) ///
				ylabel(/*0(.2)1*/, angle(0) format(%3.1g)) /*ytitle(`outcomevar_label')*/ ///
				t1title("{it:h} = `hl'; {it:N} = `obs'", size(small) place(ne) margin(zero) height(.01) ring(5)))

		}
		if `graph_i' == 3{
			cap graph drop u`days'_`runningvar'
			rdplot `outcomevar'`days' `runningvar' if abs(`runningvar') < $threshold_fg & ///
			`runningvar' ~= 0, genvars hide 
			twoway(scatter rdplot_mean_y rdplot_mean_x, ///
			title("`days' Days", size(large)) name(u`days'_`runningvar') ///
				legend(off) xtitle( {&Delta}) mc(gs10) xline(0,lc(black)) ///
				subtitle("winners　　　　　losers", size(medium) ring(5) margin(zero)) ///
				/*ytitle(`outcomevar_label')*/ ylabel(, angle(0) format(%3.1g)) ///
				graphregion(color(white) lcolor(none))) ///
			(function y = B_right[2,1]*x + B_right[1,1], range(0 .02) lwidth(thick) lc(gs4) lpattern(longdash)) ///
			(function y = B_left[2,1]*x + B_left[1,1], range(-.02 0) lwidth(thick)  lc(gs4) lpattern(longdash))
			cap drop rdplot_*
		}	
		
		if `graph_i' > 0{
			local GRAPH = "`GRAPH' u`days'_`runningvar'"
		}
	}
	di as result `"`GRAPH'"'
	return local GRAPH `"`GRAPH'"'
	return local TABLE `"`TABLE'"'
end


cap program drop drawgraph_1x2_days
program drawgraph_1x2_days
	set seed 829381
	args runningvar outcomevar condition folder title graph days
	local graph_i = 1
	local GRAPH = ""
	local TABLE = ""
	local days_f = ustrregexra("`days'"," ","_")
	di "`condition'"
	if "`condition'" ~= ""{
		local condition_f = ustrregexra("`condition'","[\s\=\!\.\,]","")
	}	 

	if "`graph'" == "nograph"{
		local graph_i = 0
	}
	else if ustrregexm("`graph'","graph_stats"){
		local graph_i = 2
	}
	else if ustrregexm("`graph'","local_linear"){
		local graph_i = 3
	}
	*graph drop _all
	preserve
		drawgraph_sub `outcomevar' `runningvar' "`condition'" "`days'" "`graph_i'" "`GRAPH'" "`TABLE'" "`buyer'"
		local GRAPH `r(GRAPH)'
		local TABLE `r(TABLE)'
	restore

	if `graph_i' > 0{
		graph combine `GRAPH', col(2) xsize(2) ysize(1) scale(.99) ///
			title("`title'") graphregion(color(white) lcolor(none))
		if ustrregexm("`graph'","noexportgraph") == 0{
			graph export ../Output/rdplot_`outcomevar'_`runningvar'`condition_f'_`days_f'.png, ///
				replace as(png) width(4096) 
		}
		cap noisily graph drop `GRAPH'
	}
	if ustrregexm("`graph'", "notable") == 0{
		esttab `TABLE' using ../Output/rdtable_`outcomevar'_`runningvar'`condition_f'_`days_f'.tex, ///
		f replace cells(none) collabels(none) title("`runningvar'") ///
		starlevels(* 0.10 ** 0.05 *** 0.01)  ///
		stats(tau_bc se_tau_rb h_r N, fmt(/*%6.0g*/3  3 3 /*3 0 0*/ "%9.0fc" ) ///
		labels(`"\raisebox{0mm}{$\hat{\beta}$}"' `" "' `"\it{h}"' `"Obs."') ///
			layout(@ (@) @ @) star(tau_bc)) ///
		mtitles("3 Months" "6 Months" "3 Months" "6 Months" )
	}
end

			

cap program drop drawgraph_11may22
program drawgraph_11may22
	set seed 829381
	args runningvar_base outcomevar condition folder title graph
	local graph_i = 1
	local GRAPH = ""
	local TABLE = ""
	if "`graph'" == "nograph"{
		local graph_i = 0
	}
	else if ustrregexm("`graph'","graph_stats"){
		local graph_i = 2
	}
	graph drop _all
	if "`condition'" ~= ""{
		keep if `condition'
		local condition_f = ustrregexra("`condition'","[\s\=\!\.\,]","")
	}	 

	foreach mode in "Delta1" "Delta2" {
		local runningvar = "`runningvar_base'1"
		if "`mode'" == "Delta2"{
			local runningvar = "`runningvar_base'2"
		}	
		di "`runningvar'"
		preserve
			drawgraph_sub `outcomevar' `runningvar' "`condition'" "90 180" "`graph_i'" "`GRAPH'" "`TABLE'"
			local GRAPH `r(GRAPH)'
			local TABLE `r(TABLE)'
		restore
	}
	if `graph_i' > 0{
		graph combine `GRAPH', col(2) xsize(2) ysize(2) scale(.55) ///
			title("`title'") graphregion(color(white) lcolor(none))
		if ustrregexm("`graph'","noexportgraph") == 0{
			graph export ../../figures/`folder'/rdplot_`outcomevar'_`runningvar_base'`condition_f'.png, ///
				replace as(png) width(4096) 
		}
	}
	if ustrregexm("`graph'", "notable") == 0{
		esttab `TABLE' using ../../tables/`folder'/rdtable_`outcomevar'_`runningvar_base'`condition_f'.tex, ///
		f replace cells(none) collabels(none) title("`runningvar'") ///
		stats(tau_bc se_tau_rb h_r /*b_l *kernel bwselect*/ N, fmt(/*%6.0g*/3  3 3 /*3 0 0*/ "%9.0fc" ) ///
		labels(`"\raisebox{0mm}{$\hat{\beta}$}"' `" "' `"\it{h}"' `"Obs."') layout(@ (@) @ @) ) ///
		mtitles("3 Months" "6 Months" /*"12 Months"*/ "3 Months" "6 Months" /*"12 Months"*/ )
	}
end







cap program drop drawgraph_comb
program drawgraph_comb
	set seed 829381
	args runningvar_base outcomevar condition folder titlex graph latexlabel caption
	local graph_i = 1
	local GRAPH = ""
	local TABLE = ""
	if "`graph'" == "nograph"{
		local graph_i = 0
	}
	else if ustrregexm("`graph'","graph_stats"){
		local graph_i = 2
	}
	graph drop _all

	foreach mode in "Delta1" "Delta2" {
		local runningvar = "`runningvar_base'"
		if "`mode'" == "Delta2"{
			local runningvar = "`runningvar_base'_placebo"
		}	
		di "`runningvar'"
		preserve
			keep if abs(`runningvar') < . 
			if "`condition'" ~= ""{
				keep if `condition'
				local condition_p = "; (`condition')"
				local condition_f = ustrregexra("`condition'","[\s\=\!\.\,]","")
			}	 
			foreach days in "90" "180" /*"365"*/{
				*di "KKKKKKKKKKKK"
				cap drop flag_tmp
				gen flag_tmp = 0
				sum `runningvar' if `runningvar' < 0, d
				replace flag_tmp = 1 if `runningvar' < `r(p10)'
				sum `runningvar' if `runningvar' > 0, d
				replace flag_tmp = 1 if `runningvar' > `r(p90)'
				rdrobust `outcomevar'`days' `runningvar' if `runningvar' ~= 0 & flag_tmp == 0, vce(nncluster contract_id)
				if _rc == 0{
					local tau = strofreal(`e(tau_bc)', "%9.2g")
					local pv = strofreal(`e(pv_rb)',"%9.2g")
					local obs = strofreal(`e(N_l)' + `e(N_r)',"%9.0fc")
					local hr = strofreal(`e(h_l)',"%9.2g")
					local hl = strofreal(`e(h_r)',"%9.2g")
					local se_tau = strofreal(`e(se_tau_rb)',"%9.2g")
				}
				local t = round(`days' / 30)
				est sto t`outcomevar'`days'`runningvar'
				local TABLE = "`TABLE' t`outcomevar'`days'`runningvar'"				
				if `graph_i' == 1{
					cap graph drop u`days'_`runningvar'
					rdplot `outcomevar'`days' `runningvar' if abs(`runningvar') < $threshold_fg & ///
					`runningvar' ~= 0, ///
					graph_options(title("`t' months") name(u`days'_`runningvar') legend(off) xtitle(`runningvar')) 
				}	
				if `graph_i' == 2{
				
					cap graph drop u`days'_`runningvar'
					rdplot `outcomevar'`days' `runningvar' if abs(`runningvar') < $threshold_fg & ///
					`runningvar' ~= 0, ///
					graph_options(title("`t' months") name(u`days'_`runningvar') legend(off) xtitle(`runningvar') ///
					/*t2title("From_`start_day'" "Thru_`end_day'", size(small) place(nw) ring(1))*/ ///
					t1title("{it:{&beta}}({it:se}):`tau' (`se_tau')", size(medsmall) place(ne) ring(1)) ///
					ylabel(/*0(.2)1*/, angle(0)) ///
					subtitle("{it:h} = `hl'; {it:N} = `obs'", size(small) place(ne) ring(0)) )
				}	
				if `graph_i' > 0{
				    local GRAPH = "`GRAPH' u`days'_`runningvar'"
				}
			}
		restore
	}
	if `graph_i' > 0{
		graph combine `GRAPH', col(2) xsize(2) ysize(2) scale(.7) ///
			title("`titlex'") 
		if ustrregexm("`graph'","noexportgraph") == 0{
			graph export "../../figures/`folder'/`titlex'_`outcomevar'_`runningvar_base'`condition_f'.png", ///
				replace as(png) width(4096) 
		}
	}
	if ustrregexm("`graph'", "notable") == 0{
		esttab `TABLE' using "../../tables/`folder'/`titlex'_`outcomevar'_`runningvar_base'`condition_f'.tex", ///
		f replace cells(none) collabels(none) title("`runningvar'") ///
		stats(tau_bc se_tau_rb h_r /*b_l *kernel bwselect*/ N, fmt(/*%6.0g*/3  3 3 /*3 0 0*/ "%9.0fc" ) ///
		labels(`"\raisebox{0mm}{$\hat{\beta}$}"' `" "' `"\it{h}"' `"Obs."') layout(@ (@) @ @) ) ///
		mtitles("3 Months" "6 Months" /*"12 Months"*/ "3 Months" "6 Months" /*"12 Months"*/ )
	}
end






program drawgraph2
	set seed 829381
	*args runningvar outcomevar condition path title graph latexlabel
	args runningvar outcomevar condition path titlex graph latexlabel caption
	local nograph = 0
	if "`graph'" == "nograph"{
		local nograph = 1
	}
	graph drop _all
	preserve
		keep if abs(`runningvar') < . /*max(real($threshold_fg),real($threshold_rd))*/
		if "`condition'" ~= ""{
			keep if `condition'
			local condition_p = "; (`condition')"
			local condition_f = ustrregexra("`condition'","[\s\=\!\.\,]","")
		}	 
		foreach d in "90" "180" "365"{
			qui sum `runningvar', d
			local t = round(`d' / 30)
			rdrobust `outcomevar'`d' `runningvar' if `runningvar' >`r(p5)' & `runningvar' < `r(p95)' & ///
			`runningvar' ~= 0, vce(nncluster contract_id)
			est sto table_`outcomevar'`d'
			/*estadd pv_rb table_util`d'*/
			if `nograph' == 0{
				cap graph drop u`d'
				rdplot `outcomevar'`d' `runningvar' if abs(`runningvar') < $threshold_fg & ///
				`runningvar' ~= 0, ///
				graph_options(title("`t' months") name(u`d') legend(off))
			}
		}
	restore
	/*estout table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365,  stats(tau_bc pv_rb runningvar outcomevar  N, fmt(3 3 0 0 0 0) ///
		 star(pv_rb) ) cells(none) mgroups(`title' `runningvar' "`condition_p'", pattern(1 1 1))
	*/
	if `nograph' == 0{
		graph combine u90 u180 u365, col(3) xsize(3) ysize(1) title("`titlex'; `runningvar'`condition_p'")
		graph export `path'/figures/`titlex'_`outcomevar'_`runningvar'`condition_f'.png, ///
			replace as(png) width(4096) 
	}
	esttab table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365 ///
		using `path'/tables/`titlex'_`outcomevar'_`runningvar'`condition_f'_independent.tex, label replace f cells(none) collabels(none) ///
		/*mgroups(`titlex' `outcomevar' "`condition_p'", pattern(1 1 1))*/ star(+ 0.10 * 0.05) ///
		stats(tau_bc pv_rb h_r /*b_l kernel bwselect*/ N, fmt(%6.0g 3 3 /*3 0 0*/ "%9.0fc" ) ///
		labels(`"\raisebox{0mm}{$\hat{\beta}$}"' `" "' `"\textit{h}"' /*`"BW Bias (b)"' `"Kernel Type"' `"BW Type"'*/ `"Obs."') ///
		star(tau_bc) layout(@ (@) @ @) ) ///
		mtitles("3 months" "6 months" "12 months") title("`runningvar'") ///
		prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{3}{c}}\hline\hline ///
					\multicolumn{4}{c}{ `caption' } \\ ) ///
			postfoot(\hline\hline\end{tabular}\caption{`caption'}\label{table`latexlabel'}})
			*postfoot(\hline\end{tabular}\caption{@title}\end{table}})
			
	esttab table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365 ///
		using `path'/tables/`titlex'_`outcomevar'_`runningvar'`condition_f'_noheader.tex, f replace cells(none) collabels(none) ///
		title("`runningvar'") ///
		stats(tau_bc pv_rb h_r /*b_l *kernel bwselect*/ N, fmt(%6.0g 3 3 /*3 0 0*/ "%9.0fc" ) ///
		labels(`"\raisebox{0mm}{$\hat{\beta}$}"' `" "' `"\textit{h}"' `"Obs."') layout(@ (@) @ @) ) ///
		mtitles("3 Months" "6 Months" "12 Months")
end


program drawbinscattergraph
	set seed 829381
	args runningvar outcomevar condition path title graph xtitle
	local nograph = 0
	if "`graph'" == "nograph"{
		local nograph = 1
	}
	graph drop _all
	preserve
		keep if abs(`runningvar') < . /*max(real($threshold_fg),real($threshold_rd))*/
		if "`condition'" ~= ""{
			keep if `condition'
			local condition_p = "; (`condition')"
			local condition_f = ustrregexra("`condition'","[\s\=\!\.\,]","")
		}	 
		foreach d in "90" "180" "365"{
			qui sum `runningvar', d
			*cap graph drop temp
			*hist `runningvar' if `runningvar' >`r(p5)' & `runningvar' < `r(p95)' , bin(200)
			rdrobust `outcomevar'`d' `runningvar' if `runningvar' >`r(p5)' & `runningvar' < `r(p95)' & ///
			`runningvar' ~= 0, vce(nncluster contract_id)
			est sto table_`outcomevar'`d'
			*estadd pv_rb table_util`d'
			if `nograph' == 0{
				cap graph drop u`d'
				binscatter `outcomevar'`d' `runningvar' if abs(`runningvar') < $threshold_fg & ///
				`runningvar' ~= 0, n(10) rd(0) line(qfit) ///
				title("`outcomevar'", size(medsmall)) name(u`d') legend(off) xline(0, lcolor(black))
			}
		}
	restore
	estout table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365,  stats(tau_bc pv_rb runningvar outcomevar  N, fmt(3 3 0 0 0 0) ///
		 star(pv_rb) ) cells(none) mgroups(`title' `runningvar' "`condition_p'", pattern(1 1 1))
	esttab table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365 ///
		using `path'/tables/`title'_`outcomevar'_`runningvar'`condition_f'.tex, f replace cells(none) collabels(none) ///
		mgroups(`title' `runningvar' "`condition_p'", pattern(1 1 1)) stats(tau_bc pv_rb runningvar outcomevar N, ///
		fmt(3 3 0 0 0 0) ///
		 labels(`"RD estimate"' `"Robust p-value"' `"Running Var"' `"Outcome Var"' `"Observations"')) ///
		mtitles("`outcomevar'90" "`outcomevar'180" "`outcomevar'365") 
		
	esttab table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365 ///
		using `path'/tables/`title'_`outcomevar'_`runningvar'`condition_f'_nolabel.tex, f replace cells(none) collabels(none) ///
		mgroups(`title' "`condition_p'", pattern(1 0 1)) stats(tau_bc pv_rb runningvar outcomevar N, ///
		fmt(3 3 0 0 0 0) labels(none)) mtitles("`outcomevar'90" "`outcomevar'180" "`outcomevar'365")
	if `nograph' == 0{
		graph combine u90 u180 u365, col(3) xsize(3) ysize(1) title("`title'; `runningvar'`condition_p'")
		graph export `path'/figures/`title'_`outcomevar'_`runningvar'`condition_f'.png, ///
			replace as(png) width(4096) 
	}
	esttab table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365 ///
		using `path'/tables/`title'_`outcomevar'_`runningvar'`condition_f'2.tex, f replace cells(none) collabels(none) ///
		mgroups(`title' "`condition_p'", pattern(1 0 1)) stats(tau_bc pv_rb h_r b_l kernel bwselect N, ///
		fmt(%6.0g 3 3 3 0 0 0 ) ///
		 labels(`"RD estimate"' `"Robust p-value"' `"BW Loc. Poly. (h)"' `"BW Bias (b)"' `"Kernel Type"' `"BW Type"' `"Observations"')) ///
		mtitles("`outcomevar'90" "`outcomevar'180" "`outcomevar'365") title("`runningvar'") ///
		prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{3}{c}}\hline\hline) ///
			postfoot(\hline\hline\end{tabular}\caption{@title}})
			*prehead({\def\arraystretch{1.3}\begin{table}[t]\centering\begin{tabular}{l*{3}{c}}\hline\hline) ///
			*postfoot(\hline\end{tabular}\caption{@title}\end{table}})

	esttab table_`outcomevar'90 table_`outcomevar'180 table_`outcomevar'365 ///
		using `path'/tables/`title'_`outcomevar'_`runningvar'`condition_f'2_nolabel.tex, f replace cells(none) collabels(none) ///
		title("`runningvar'") mgroups(`title', pattern(0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
		stats(tau_bc pv_rb h_r b_l kernel bwselect N,	fmt(%6.0g 3 3 3 0 0 0 ) labels(none)) ///
		mtitles("`outcomevar'90" "`outcomevar'180" "`outcomevar'365") postfoot(\hline\hline\end{tabular}\caption{@title}}) ///
		prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{3}{c}}\hline\hline) ///
			

	
end

/*estpost sum bidder_muni_N bidder_muni_won mean_blog90 mean_blog180 ///
		if base == 1 & bidder_muni_n2 == 1, listwise  /* dont need FLAG == 0 because bidder_muni_n2 is missing unless FLAG == 0 101619*/
		est store a1
			esttab a a1 using Output/sumres.tex, title("Summary Statistics of Auctions and Bidders.") cells("mean(fmt(%06.3fc)) sd(par fmt(%6.3fc))") ///
				label replace brackets mtitle("By Auctions " "By Bidders") collabels(none) stats(N, labels("Obs.") fmt(%8.0gc)) ///
					prehead({\def\arraystretch{1.3}\begin{table}[t]\centering\begin{tabular}{l*{3}{cc}}\hline\hline) ///
			postfoot(\hline\end{tabular}\caption{@title}\end{table}})
*/

program drawgraph_si
	set seed 102847
	args runningvar outcomevar condition path title
	graph drop _all
	preserve
		keep if abs(`runningvar') < . /*max(real($threshold_fg),real($threshold_rd))*/
		if "`condition'" ~= ""{
			keep if `condition'
			local condition_p = "; (`condition')"
			local condition_f = ustrregexra("`condition'","[\s\=\!\.\,]","")
		}	 
			rdrobust `outcomevar' `runningvar' if `runningvar' >`r(p5)' & `runningvar' < `r(p95)' & ///
			`runningvar' ~= 0, vce(nncluster contract_id)
			/*rdrobust `outcomevar' `runningvar' if abs(`runningvar') < $threshold_rd & ///
			`runningvar' ~= 0, vce(nncluster contract_id)*/
			est sto table_util
			rdplot `outcomevar' `runningvar' if abs(`runningvar') < $threshold_fg & ///
				`runningvar' ~= 0, ///
			 graph_options(title("`outcomevar'", size(medsmall)) name(u) legend(off))
	restore
	esttab table_util, stats(N pv_rb)
	esttab table_util using `path'/tables/`title'_`outcomevar'_`runningvar'`condition_f'.tex, f cells(none) ///
		stats(tau_bc pv_rb runningvar outcomevar N, fmt(3 3 0 0 0 0) ///
		labels(`"RD estimate"' `"Robust p-value"' `"Running Var"' `"Outcome Var"' `"Observations"')) ///
			mtitles("`outcomevar'") title("`title'; `runningvar'`condition_p'") replace collabels(none) 
	esttab table_util using `path'/tables/`title'_`outcomevar'_`runningvar'`condition_f'_nolabel.tex, f cells(none) ///
		stats(tau_bc pv_rb runningvar outcomevar  N, fmt(3 3 0 0 0 0) labels(none))  ///
			mtitles("`outcomevar'") title("`title'; `runningvar'`condition_p'") replace collabels(none) 
	graph combine u, xsize(1) ysize(1) title("`title'; `runningvar'`condition_p'")
	graph export `path'/figures/`title'_`outcomevar'_`runningvar'`condition_f'.png, ///
		replace as(png) width(4096) 
end
